﻿#include "actiondialog.h"
#include "ui_actiondialog.h"
#include <QFileDialog>
#include <QCoreApplication>
#include <QDir>
#include <QFile>
#include <QIcon>

#include <QDomDocument>
#include <QTreeWidgetItem>

ActionDialog::ActionDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ActionDialog)
{
    ui->setupUi(this);
    this->setWindowTitle(tr("ActionDialog"));
    this->setObjectName("WidgetActionDialogObject");

    mCurPage = QString();
    mCurGroup = QString();

#if 1
    ui->checkBoxDefinePage->setVisible(false);
    ui->checkBox_DefineGroup->setVisible(false);
    ui->lineEdit_ActionPage->setVisible(false);
    ui->lineEdit_ActionGroup->setVisible(false);
#endif
    // 初始化菜单page和group
    initPage();
}

ActionDialog::~ActionDialog()
{
    delete ui;
}

void ActionDialog::updateTreeItem(QTreeWidgetItem *pTreeItem)
{
    if(nullptr == pTreeItem)return;
    // name
    pTreeItem->setData(0,Qt::WhatsThisRole,ui->comboBox->currentIndex());
    pTreeItem->setData(0,Qt::DisplayRole,ui->lineEdit_ActionText->text());

    // page
    pTreeItem->setData(1,Qt::WhatsThisRole,mCurPage);
    pTreeItem->setData(1,Qt::DisplayRole,ui->comboBox_ActionPage->currentText());

    // group
    pTreeItem->setData(2,Qt::WhatsThisRole,mCurGroup);
    pTreeItem->setData(2,Qt::DisplayRole,ui->comboBox_ActionGroup->currentText());

    // context
    pTreeItem->setData(3,Qt::WhatsThisRole,ui->lineEdit_IconPath->text());
    pTreeItem->setData(3,Qt::DisplayRole,ui->lineEdit_ActionContext->text());

    pTreeItem->setIcon(0,QIcon(ui->lineEdit_IconPath->text()));
}

QString ActionDialog::actionText()
{
    return ui->lineEdit_ActionText->text();
}

QString ActionDialog::actionContext()
{
    return ui->lineEdit_ActionContext->text();
}

//bool ActionDialog::actionCheckable()
//{
//    return ui->checkBox_ActionCheckable->isChecked();
//}

//bool ActionDialog::actionCheckedDefault()
//{
//    return ui->checkBox_ActDefaultChecked->isChecked();
//}

QString ActionDialog::actionIcon()
{
    return ui->lineEdit_IconPath->text();
}

int ActionDialog::actionStyle()
{
    return ui->comboBox->currentIndex();
}

QString ActionDialog::actionPage()
{
    if(ui->checkBoxDefinePage->isChecked())
    {
        return ui->lineEdit_ActionPage->text();
    }
    else {
        return ui->comboBox_ActionPage->currentText();
    }
}

QString ActionDialog::actionGroup()
{
    if(ui->checkBoxDefinePage->isChecked())
    {
        return ui->lineEdit_ActionGroup->text();
    }
    else {
        return ui->comboBox_ActionGroup->currentText();
    }
}

void ActionDialog::initPage()
{
    QDir dir(QCoreApplication::applicationDirPath());
    dir.cdUp();
    dir.cd("config");
    QFile file(dir.absoluteFilePath("menuconfig.xml"));
    if(!file.open(QIODevice::ReadOnly))return;
    QDomDocument doc;
    doc.setContent(&file);
    file.close();

    QDomElement hroot = doc.firstChildElement("hroot");
    if(hroot.isNull())return;
    QDomElement category = hroot.firstChildElement("category");
    while (!category.isNull()) {
        QString sPageEn = category.attribute("en");
        QString sPageCh = category.attribute("ch");
        mapEnCh.insert(sPageEn,sPageCh);
        QDomElement pannel = category.firstChildElement("pannel");
        QStringList lstPannel;
        lstPannel.clear();
        while (!pannel.isNull()) {
            QString sPannelEn = pannel.attribute("en");
            QString sPannelCh = pannel.attribute("ch");
            mapEnCh.insert(sPannelEn,sPannelCh);
            lstPannel.append(sPannelEn);
            pannel = pannel.nextSiblingElement("pannel");
        }
        mapEnName.insert(sPageEn,lstPannel);
        category = category.nextSiblingElement("category");
    }

    ui->comboBox_ActionPage->clear();
    QStringList lstPageEn = mapEnName.keys();
    foreach (QString s, lstPageEn) {
        ui->comboBox_ActionPage->addItem(mapEnCh.value(s));
    }
    ui->comboBox_ActionPage->setCurrentIndex(0);
}

void ActionDialog::on_toolButton_selectIcon_clicked()
{
    QString sFileName = QFileDialog::getOpenFileName(this,QString(),QDir::currentPath(),QString("*.png"));

    ui->lineEdit_IconPath->setText(sFileName);

    ui->label_Icon->setPixmap(QPixmap(sFileName));
}

void ActionDialog::on_lineEdit_ActionText_textChanged(const QString &arg1)
{
    Q_UNUSED(arg1)
//    ui->lineEdit_ActionPage->setText(arg1);
//    ui->lineEdit_ActionGroup->setText(arg1);
}

void ActionDialog::on_checkBoxDefinePage_stateChanged(int arg1)
{
    if(arg1)
    {
        ui->comboBox_ActionPage->setVisible(false);
        ui->lineEdit_ActionPage->setEnabled(true);
    }
    else {
        ui->comboBox_ActionPage->setVisible(true);
        ui->lineEdit_ActionPage->setEnabled(false);
    }
}

void ActionDialog::on_checkBox_DefineGroup_stateChanged(int arg1)
{
    if(arg1)
    {
        ui->comboBox_ActionGroup->setVisible(false);
        ui->lineEdit_ActionGroup->setEnabled(true);
    }
    else {
        ui->comboBox_ActionGroup->setVisible(true);
        ui->lineEdit_ActionGroup->setEnabled(false);
    }
}

void ActionDialog::on_comboBox_ActionPage_currentIndexChanged(const QString &arg1)
{
    ui->comboBox_ActionGroup->clear();
    QStringList lstEn = mapEnCh.keys();
    foreach (QString s, lstEn) {
        if(mapEnCh.value(s) == arg1)
        {
            mCurPage = s;
            QStringList lstGroup = mapEnName.value(s);
            foreach (QString sgroup, lstGroup) {
                ui->comboBox_ActionGroup->addItem(mapEnCh.value(sgroup));
            }
            break;
        }
        else {
            continue;
        }
    }
}

void ActionDialog::on_comboBox_ActionGroup_currentIndexChanged(const QString &arg1)
{
    if(mCurPage.isEmpty())mCurGroup = QString();

    QStringList lstGroup = mapEnName.value(mCurPage);

    foreach (QString s, lstGroup) {
        if(mapEnCh.value(s) == arg1){
            mCurGroup = s;
        }
    }
}
